import { v4 } from 'node-uuid';

// This is a fake in-memory implementation of something
// that would be implemented by calling REST server.

const fakeDatabase = {
  todos: [
    {
      id: v4(),
      text: 'hey',
      completed: true
    },
    {
      id: v4(),
      text: 'ho',
      completed: true
    },
    {
      id: v4(),
      text: 'let’s go',
      completed: false
    }
  ]
};

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

export const fetchTodos = (filter) =>
  delay(500).then(() => {
    switch (filter) {
      case 'all':
        return fakeDatabase.todos;
      case 'uncompleted':
        return fakeDatabase.todos.filter((t) => !t.completed);
      case 'completed':
        return fakeDatabase.todos.filter((t) => t.completed);
      default:
        throw new Error(`Unknown filter: ${filter}`);
    }
  });

export const toggleTodo = (id) =>
  delay(500).then(() => {
    const todo = fakeDatabase.todos.find((t) => t.id === id);
    todo.completed = !todo.completed;
    return todo;
  });

export const addTodo = (text) =>
  delay(500).then(() => {
    const todo = {
      id: v4(),
      text,
      completed: false
    };
    fakeDatabase.todos.push(todo);
    return todo;
  });
